package cn.wolfcode.business.service.impl;

import java.util.List;

import cn.wolfcode.business.domain.Appointment;
import cn.wolfcode.business.mapper.AppointmentMapper;
import cn.wolfcode.business.mapper.StatementItemMapper;
import cn.wolfcode.business.utils.RegexUtils;
import cn.wolfcode.business.utils.VehiclePlateNoUtil;
import cn.wolfcode.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wolfcode.business.mapper.StatementMapper;
import cn.wolfcode.business.domain.Statement;
import cn.wolfcode.business.service.IStatementService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 结算单Service业务层处理
 *
 * @author 白金之星
 * @date 2023-10-31
 */
@Service
@Transactional
public class StatementServiceImpl implements IStatementService {
    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;

    /**
     * 查询结算单
     *
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public Statement selectStatementById(Long id) {
        return statementMapper.selectStatementById(id);
    }

    /**
     * 查询结算单列表
     *
     * @param statement 结算单
     * @return 结算单
     */
    @Override
    public List<Statement> selectStatementList(Statement statement) {
        return statementMapper.selectStatementList(statement);
    }

    /**
     * 新增结算单
     *
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int insertStatement(Statement statement) {
        if (statement == null) {
            throw new RuntimeException("非法参数");
        }
        //手机号码校验
        Assert.state(RegexUtils.isPhoneLegal(statement.getCustomerPhone()), "手机号码格式不正确");
        //车牌号校验
        Assert.notNull(VehiclePlateNoUtil.getVehiclePlateNo(statement.getLicensePlate()), "车牌号不正确");
        //日期校验
        Assert.isTrue(statement.getActualArrivalTime().before(DateUtils.getNowDate()), "日期时间不合理");

        statement.setCreateTime(DateUtils.getNowDate());
        return statementMapper.insertStatement(statement);
    }

    /**
     * 修改结算单
     *
     * @param statement 结算单
     * @return 结果
     */
    @Override
    public int updateStatement(Statement statement) {
        if (statement == null) {
            throw new RuntimeException("非法参数");
        }
        //手机号码校验
        Assert.state(RegexUtils.isPhoneLegal(statement.getCustomerPhone()), "手机号码格式不正确");
        //车牌号校验
        Assert.notNull(VehiclePlateNoUtil.getVehiclePlateNo(statement.getLicensePlate()), "车牌号不正确");
        //日期校验
        Assert.isTrue(statement.getActualArrivalTime().before(DateUtils.getNowDate()), "日期时间不合理");
        //必须是消费中才能修改
        Statement statement1 = statementMapper.selectStatementById(statement.getId());
        if (!statement1.getStatus().equals(Statement.STATUS_CONSUME)) {
            throw new RuntimeException("必须要处于消费中状态");
        }
        return statementMapper.updateStatement(statement);
    }

    /**
     * 批量删除结算单
     *
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementByIds(Long[] ids) {
        for (Long id : ids) {
            Statement statement = statementMapper.selectStatementById(id);
            if(statement == null){
                throw new RuntimeException("参数异常");
            }
            //结算单：将is_delete改为已删除
            statement.setIsDelete(Statement.IS_DELETE_YES);
            statementMapper.updateStatement(statement);
            //结算单明细，直接删除
            statementItemMapper.deleteStatementItemById(id);
            //预约单改为到店状态
            if(statement.getAppointmentId() != null){
                appointmentMapper.updateAuditStatus(statement.getAppointmentId(), Appointment.STATUS_ARRIVAL);
            }
        }
        return 1;
    }

    /**
     * 删除结算单信息
     *
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteStatementById(Long id) {
        return statementMapper.deleteStatementById(id);
    }
}
